﻿using System.Collections.Generic;
using System.Linq;
using Socium.Domain.Collections;
using Socium.Domain.Persons;
using Socium.Domain.Tests;
using Socium.Domain.Tests.Testings;

namespace Socium.Testings {
	class TestTreeItem : TreeItemBase<Test, IQuestionTreeItem> {
		public TestTreeItem(Test model) : base(model, model.Name) {}

		//static IQuestionTreeItem CreateQuestionTreeItem(Question question) {
		//    var closed_question = question as ClosedQuestion;
		//    if (closed_question != null) {
		//        return CreateQuestionTreeItem(closed_question);
		//    }

		//    return null;
		//}

		static IQuestionTreeItem CreateQuestionTreeItem(ClosedQuestion question) {
			var count = question.AnswerCountRange;
			if (count.IsOne) {
				return new SingleQuestionTreeItem(question);
			}
			
			return new MultipleQuestionTreeItem(question);
		}

		protected override IEnumerable<IQuestionTreeItem> GetChilderen(Test model) {
			var questions = model.Questions.OfType<ClosedQuestion>();
			return questions.Select(CreateQuestionTreeItem);
		}

		public override void Save(Person person, Testing testing) {
			Childeren.Iterate(item => item.Save(person, testing));
		}

		public override void Load(Person person, Testing testing) {
			Childeren.Iterate(item => item.Load(person, testing));
		}
	}
}
